<?php
namespace app\index\controller;
use think\Db;

class Article extends Common
{
    //文章列表及详情
    public function index()
    {
        //侧边栏分类导航
        $cid=input('cid');
        $cate_info=DB::name('category')->where('id',$cid)->find();
        if($cate_info['pid']!=0){
            $pcate_info=DB::name('category')->where('id',$cate_info['pid'])->find();
            $cate_child=DB::name('category')->where('pid',$pcate_info['id'])->where('status',1)->order('displayorder desc')->select();
            $this->assign('pcate_info',$pcate_info);
            $this->assign('cate_child',$cate_child);
        }

        //文章列表
        $article_list=DB::name('article')->where('cid',$cid)->where('status',1)->order('displayorder desc, updatetime desc')->select();

        //单篇文章
        $aid=input('aid');
        if(isset($aid) ||$cate_info['type']==4)
        {
            if(isset($aid)){
                $article_info=DB::name('article')->where('id',$aid)->find();
                //阅读数自增
                DB::name('article')->where('id',$aid)->setInc('views');
            }
            if(!isset($article_info)){
                $article_info=DB::name('article')->where('cid',$cid)->find();
            }
            $article_info['updatetime']=date('Y-m-d',$article_info['updatetime']);
            $this->assign('aid',$aid);
            $this->assign('article_info',$article_info);
        }

        $sex=input('sex');
        $phone=input('phone');
        $babyname=input('babyname');
        $birthday=input('birthday');
        isset($sex)&&$this->assign('sex',$sex);
        isset($phone)&&$this->assign('phone',$phone);
        isset($babyname)&&$this->assign('babyname',$babyname);
        isset($birthday)&&$this->assign('birthday',$birthday);
        $this->assign('cid',$cid);
        $this->assign('cate_info',$cate_info);
        $this->assign('article_list',$article_list);
        return $this->fetch();
    }

    //预约表单提交
    public function register()
    {
        $postdata=input('post.');
        $postdata['addtime']=time();
        $ip=$this->get_real_ip();
        $postdata['ip']=$ip;
        $find=DB::name('register')->where('ip',$ip)->where('phone',$postdata['phone'])->find();
        if($find){
            DB::name('register')->where('ip',$ip)->update($postdata);
        }else{
            DB::name('register')->insert($postdata);
        }
        $history=$_SERVER['HTTP_REFERER'].'&record=1&phone='.$postdata['phone'].'&babyname='.$postdata['babyname'].'&birthday='.$postdata['birthday'].'&sex='.$postdata['sex'];
        header('location:'.$history);
    }


}
